import * as vscode from 'vscode';
import * as path from 'path';

export class TerminalManager {
    private terminal: vscode.Terminal | null = null;

    runCommand(command: string, onComplete: (outputFile: string) => void) {
        if (!this.terminal) {
            this.terminal = vscode.window.createTerminal('Manim Runner');
        }
        this.terminal.show();
        this.terminal.sendText(command);

        // 模拟渲染完成（实际应监听文件生成或终端输出）
        setTimeout(() => {
            const fileName = path.basename(command.split(' ')[3]); // 提取文件名
            const sceneName = command.split(' ')[4]; // 提取场景名
            const quality = command.match(/-q(\w+)/)?.[1] || 'l'; // 提取清晰度
            const outputFile = path.join('media', 'videos', fileName, quality === 'l' ? '480p15' : quality === 'm' ? '720p30' : '1080p60', `${sceneName}.mp4`);
            onComplete(outputFile);
        }, 30000); // 模拟30秒渲染时间
    }

    dispose() {
        if (this.terminal) {
            this.terminal.dispose();
            this.terminal = null;
        }
    }
}